<!--
 * @Date: 2021-10-27 09:15:50
 * @LastEditors: 邓春宜
 * @Desc: 圆角带刻度仪表图样式1
 * @LastEditTime: 2021-10-29 12:02:04
 * @FilePath: \tianjushi-kanban\src\components\charts\gauge1.vue
-->
<template>
<div :style="[{ width: width, height: height,overflow:'hidden' }]">
  <div
    ref="echartref"
    :style="[{ width: width, height: height }, _resetChartStyle]"
  ></div>
</div>
</template>

<script>
import * as echarts from "echarts";

import { onMounted,toRefs, ref,reactive,watch,computed } from 'vue';
import { resetChartStyle, showLoading } from "@/core/tools/common";
export default {
  
  props: {
      title: {
      type: String,
      default: "100%",
    },
      width: {
      type: String,
      default: "100%",
    },
     height: {
      type: String,
      default: "100%",
    },
    total: {
      type: Number,
      default: 100,
    },
    value: {
      type: Number,
      default: 50,
    },
    color: {
      type: Array,
      default: ["#43B7FF","#009DFF"],
    },
  },
  setup: function (props, context) {
     
    /*监听props*/
    watch(props, (newProps, oldProps) => {
        f_echartInit(state.chartInit)
    }); 
 
    let state = reactive({ 
      echartref: ref(),
      chartInit:""
    }) 

    const _resetChartStyle = computed({
      get() {
        return resetChartStyle()
      } 
    }) 

    const f_echartInit = (chartdom) =>{ 
     
      // 指定图表的配置项和数据
        
      /* ------------------begin------------------ */
      let data_chart0 = {
        total: props.total,
        value: props.value,
      };
     let option = {
        grid: {
          left: "10",
          right: "10",
          bottom: "10",
        },
        series: [
          //刻度线
          {
            center: ["50%", "55%"], //仪表的位置
            name: "刻度",
            type: "gauge",
            radius: "95%",
            min: 0,
            max: data_chart0.total,
            splitNumber: 5, //刻度数量
            startAngle: 220,
            endAngle: -40,
            axisLine: {
              show: true,
              lineStyle: {
                width: 1,
                color: [[1, "rgba(0,0,0,0)"]],
              },
            }, //仪表盘轴线
            axisLabel: {
              show: true,
              color: "#cbcbcb",
              distance: -20,
              textStyle: {
                fontSize: 16,
              },
            }, //刻度标签。
            axisTick: {
              show: true,
              lineStyle: {
                color: "#cbcbcb",
                width: 1,
              },
              length: -5,
            }, //刻度样式
            splitLine: {
              show: true,
              length: -10,
              lineStyle: {
                color: "#cbcbcb",
              },
            }, //分隔线样式
            detail: {
              show: false,
            },
            pointer: {
              show: false,
            },
          },
          {
            //背景
            type: "gauge",
            radius: "85%",
            center: ["50%", "55%"],
            splitNumber: 0, //刻度数量
            startAngle: 220,
            endAngle: -40,
            show: true,
            axisLine: {
              show: true,
              roundCap: true,
              lineStyle: {
                width: 30,
                shadowColor: "rgba(0, 0, 0, 0.1)",
                shadowBlur: 10,
                shadowOffsetY: 70,
                color: [[1, "#14306F"]],
              },
            },
            //分隔线样式。
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            pointer: {
              show: false,
            },
          },
          {
            type: "gauge",
            radius: "85%",
            center: ["50%", "55%"],
            splitNumber: 0, //刻度数量
            startAngle: 220,
            endAngle: -40,
            show: true,
            axisLine: {
              show: true,
              roundCap: true,
              lineStyle: {
                width: 30,
                color: [
                  [
                    data_chart0.value / data_chart0.total,
                    new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: props.color[0],
                      },
                      {
                        offset: 0.5,
                        color: props.color[0],
                      },
                      {
                        offset: 1,
                        color: props.color[1],
                      },
                    ]),
                  ],
                ],
              },
            },
            //分隔线样式。
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            pointer: {
              show: false,
            },
            title: {
              show: true,
              offsetCenter: [0, "25%"], // x, y，单位px
              textStyle: {
                color: "#cdcdcd",
                fontSize: 18,
              },
            },
            //仪表盘详情，用于显示数据。
            detail: {
              formatter: "{value}",
              offsetCenter: ["0", "-10%"],
              textStyle: {
                fontSize: 40,
                color: "#FFFFFF",
              },
            },
            data: [
              {
                name: props.title,
                value: data_chart0.value,
              },
            ],
          },
        ],
      };

      chartdom.setOption(option);
      chartdom.hideLoading(); 
    }
    //挂载
    onMounted(()=>{ 
      state.chartInit = echarts.init(state.echartref); 
      state.chartInit.showLoading(showLoading());
      f_echartInit(state.chartInit)
    }) 
    return {
      ...toRefs(state), 
      _resetChartStyle
    };  

  }, 
 
};
</script>

<style>
</style>